package cn.wzb.hrm.service.impl;

import cn.wzb.hrm.doc.CourseDoc;
import cn.wzb.hrm.domain.Course;
import cn.wzb.hrm.domain.CourseDetail;
import cn.wzb.hrm.domain.CourseMarket;
import cn.wzb.hrm.dto.CourseDto;
import cn.wzb.hrm.feignclient.ESFeignClient;
import cn.wzb.hrm.mapper.CourseDetailMapper;
import cn.wzb.hrm.mapper.CourseMapper;
import cn.wzb.hrm.mapper.CourseMarketMapper;
import cn.wzb.hrm.query.CourseQuery;
import cn.wzb.hrm.service.ICourseService;
import cn.wzb.hrm.util.AjaxResult;
import cn.wzb.hrm.util.PageList;
import cn.wzb.hrm.util.UserContext;
import cn.wzb.hrm.vo.LoginInFo;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMarketMapper courseMarketMapper;

    @Autowired
    private CourseDetailMapper courseDetailMapper ;

    @Autowired
    private ESFeignClient esFeignClient;

    @Override
    public void saveOrUpdate(CourseDto courseDto) {
        Course course = courseDto.getCourse();
        CourseMarket courseMarket = courseDto.getCourseMarket();
        CourseDetail courseDetail = courseDto.getCourseDetail();

        //判断参数
        if(!StringUtils.hasLength(course.getName())){
            throw new RuntimeException("课程名字不能为空");
        }
        if(!StringUtils.hasLength(course.getUsers())){
            throw new RuntimeException("适用人群不能为空");
        }

        //保存课程表
        //状态
        course.setStatus(Course.STATUS_NOTONLINE);
        //获取当前用户
        LoginInFo loginInFo = UserContext.getLoginInFo();

        //机构id
        course.setTenantId(loginInFo.getTenantId());
        //机构名字
        course.setTenantName(loginInFo.getTenantName());
        //用户id
        course.setUserId(loginInFo.getUserId());
        //用户名字
        course.setUserName(loginInFo.getUserName());
        //课程类型的名字字段设置值
//        course.setGradeName("黄金");
        Long gradeId = course.getGradeId();
        if(gradeId==1L){
            course.setGradeName("黑铁");
        }else if(gradeId==2L){
            course.setGradeName("青铜");
        }else if(gradeId==3L){
            course.setGradeName("白银");
        }else if(gradeId==4L){
            course.setGradeName("黄金");
        }

        Date date = new Date();
        course.setStartTime(date);
        course.setEndTime(date);
        baseMapper.insert(course);

        //3.保存课程营销表
        courseMarket.setId(course.getId());
        courseMarket.setValid(0);
        courseMarket.setExpires(date);
        //获取前台传过来的值
        Float price = courseMarket.getPrice();
        System.out.println("课程价格---------------"+price);
        //如果不为空 就设置价格
        if(price!=null){
            courseMarket.setPriceOld(courseMarket.getPrice()+100);
            //如果为空就设置原价为空
        }else {
            courseMarket.setPriceOld(null);
        }

        //
        courseMarketMapper.insert(courseMarket);

        //4.保存课程详情表
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
    }

    @Override
    public void onLineCourse(Long id) {
        //根据id查询课程对象
        Course course = baseMapper.selectById(id);
        //判断--课程是否为空,状态是否是下线
        if(course==null || course.getStatus()!=Course.STATUS_NOTONLINE){
            throw new RuntimeException("课程为空或者课程状态异常");
        }
        //把课程存到ES中
        //3.把课程存储到ES
        //3.1.编写ES的Feign
        //3.2.把课程 Course 转成 CourseDoc

        //课程营销
        CourseMarket courseMarket = courseMarketMapper.selectById(id);

        //课程详情
        CourseDetail courseDetail = courseDetailMapper.selectById(id);
        //CourseDoc需要有哪些内容
        CourseDoc courseDoc = new CourseDoc();

        //属性拷贝
        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);
        BeanUtils.copyProperties(courseDetail,courseDoc);

        // 门户网站需要什么内容我们就放什么内容到CourseDoc
        //3.3.调用ES的Feign添加CourseDoc数据到ES
        AjaxResult saveAjaxResult = esFeignClient.onLineCourse(courseDoc);
        if(!saveAjaxResult.isSuccess()){
            throw new RuntimeException("课程上线失败");
        }

        //修改数据库中的状态
        course.setStatus(Course.STATUS_ONLINE);
        baseMapper.updateById(course);

    }

    @Override
    public void offLineCourse(Long id) {
        Course course = baseMapper.selectById(id);
        //判断--课程是否为空
        if(course == null ){
            throw new RuntimeException("无效的课程");
        }

        //判断课程状态是否上线
        if(course.getStatus()!=Course.STATUS_ONLINE){
            throw new RuntimeException("课程状态无效");
        }

        //删除es中的课程
        AjaxResult ajaxResult = esFeignClient.deleteById(id);
        if(!ajaxResult.isSuccess()){
            throw new RuntimeException("课程下线失败");
        }

        //修改数据库中的状态
        course.setStatus(Course.STATUS_NOTONLINE);
        baseMapper.updateById(course);

    }

    @Override
    public PageList<CourseDoc> queryCourses(CourseQuery courseQuery) {
        return esFeignClient.searchCourse(courseQuery);
    }
}
